<?php

/**
*
*/
class zhou_positionController extends admin
{
    public $zhou_sima;
    function __construct()
    {
       parent::__construct();
       $this->zhou_sima = $this->model('zhou_sima');
    }
    //职位列表
    public function position_listAction(){
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        $pagelist = $this->instance('pagelist');    //加载分页类
        $pagesize = 10;
        $pagelist->loadconfig();
        $urlparam = array('page' => '{page}');
        $total = $this->zhou_sima->get_count('zhou_position','position_id',' 1');
        $pagelist = $pagelist->total($total)->url(url('admin/zhou_position/position_list', $urlparam))->num($pagesize)->page($page)->output();
        $res = $this->zhou_sima->get_one_page($page,$pagesize,$total,'zhou_position',' 1 order by position_id asc');
        $this->view->assign(array(
            'res'=>$res,
            'pagelist'=>$pagelist
            ));
        $this->view->display('admin/zhou_position_list');
    }
    //职位添加、编辑
    public function position_editAction(){
        $id = $this->get('edit_id');
        $table = 'zhou_position';
        if ($id) {
            $res = $this->zhou_sima->get_one($table,'position_id ='.$id);
            $this->view->assign('res',$res);
        }
        if ($_POST) {
            $data = array();
            $data['position_name'] = $_POST['name'];
            $data['position_addtime'] = time();
            if ($id) {//编辑
                $list = $this->zhou_sima->update_table($table,$data,'position_id ='.$id);
            }else{
                $list = $this->zhou_sima->db_add($table,$data);
            }
            if ($list) {
                $this->adminMsg(lang('success'),url('admin/zhou_position/position_list'),2,1,1);
            }
        }
        $this->view->display('admin/zhou_position_edit');
    }

    //删除
    public  function position_delAction(){
        $del_id = $this->get('del_id');
        if ($del_id){
            $res = $this->zhou_sima->del('zhou_position','position_id ='.$del_id);
            if($res){
                $this->adminMsg(lang('success'),url('admin/zhou_position/position_list'),2,1,1);
            }
        }
    }

    //工种管理
    public  function work_listAction(){
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        $pagelist = $this->instance('pagelist');    //加载分页类
        $pagesize = 10;
        $pagelist->loadconfig();
        $urlparam = array('page' => '{page}');
        $total = $this->zhou_sima->get_count('zhou_work_type','work_id',' 1');
        $pagelist = $pagelist->total($total)->url(url('admin/zhou_position/position_list', $urlparam))->num($pagesize)->page($page)->output();
        $res = $this->zhou_sima->get_one_page($page,$pagesize,$total,'zhou_work_type',' 1 order by work_id asc');
        $this->view->assign(array(
            'res'=>$res,
            'pagelist'=>$pagelist
        ));
        $this->view->display('admin/zhou_work_list');
    }
      //工种添加。编辑
    public function work_editAction(){
        $id = $this->get('edit_id');
        $table = 'zhou_work_type';
        if ($id) {
            $res = $this->zhou_sima->get_one($table,'work_id ='.$id);
            $this->view->assign('res',$res);
        }
        if ($_POST) {
//            var_dump($_POST);die;
            $data = array();
            $data['work_name'] = $_POST['name'];
            $data['addtime'] = time();
            if ($id) {//编辑
                $list = $this->zhou_sima->update_table($table,$data,'work_id ='.$id);
            }else{
                $list = $this->zhou_sima->db_add($table,$data);
            }
            if ($list) {
                $this->adminMsg(lang('success'),url('admin/zhou_position/work_list'),2,1,1);
            }
        }
        $this->view->display('admin/zhou_work_edit');
    }
    //删除
    public  function work_delAction(){
        $del_id = $this->get('del_id');
        if ($del_id){
            $res = $this->zhou_sima->del('zhou_work_type','work_id ='.$del_id);
            if($res){
                $this->adminMsg(lang('success'),url('admin/zhou_position/work_list'),2,1,1);
            }
        }
    }

    //导入地区数据
    public  function dao_areaAction(){
        $table = 'zhou_area';
        if ($_POST) {//1覆盖，
//            var_dump($_POST);die;
            if ($_POST['fugai'] == 1) {
                $res= $this->zhou_sima->db_truncate($table);
            }
            $xls_url = $_POST['start_xls'];
            if (empty($xls_url)) {
                // 提醒
                die();
            }
            // die();
            require_once 'Classes/PHPExcel.php';
            require_once 'Classes/PHPExcel/IOFactory.php';
            require_once 'Classes/PHPExcel/Reader/Excel5.php';
            $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
            $objPHPExcel = $objReader->load($xls_url); //$filename可以是上传的文件，或者是指定的文件
            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow(); // 取得总行数
            $highestColumn = $sheet->getHighestColumn(); // 取得总列数
            $k = 0;
            //循环读取excel文件,读取一条,插入一条
            $a = array();
            $b = array();
            $c = array();
            for($j=1;$j<=$highestRow;$j++)
            {
                $a1 = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//获取A列的值
                $b1 = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//获取B列的值
                $c1 = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//获取B列的值
                // if ($a1!=NULL || $a1 !='') {
                $a[] = $a1;
                // }
                // if ($b1!=NULL || $b1 !='') {
                $b[] = $b1;
                // }
                // if ($c1!=NULL || $c1 !='') {
                $c[] = $c1;
                // }
            }

            foreach ($a as $key => $value) {
                $res = $this->zhou_sima->get_one('zhou_area',"area_name = '$value'");
//           var_dump($b);die;
                if (empty($res)) {
                    $arr = array();
                    $arr['area_name'] = $value;
                    $arr['area_p_id'] = 0;
                    $arr['area_show'] = 1;
                    $arr['area_addtime'] = time();
                    $id = $this->zhou_sima->db_add_re_insert_id('zhou_area',$arr);
//                    var_dump($id);die;
                    if ($id !=false) {
                        if ($b[$key] == '') {

                        }else{
                            $brr = array();
                            $brr['area_name'] = $b[$key];
                            $brr['area_p_id'] = $id;

                            $brr['area_show'] = 1;
                            $brr['area_addtime'] = time();
                            $id = $this->zhou_sima->db_add_re_insert_id('zhou_area',$brr);
                            if ($c[$key] != '') {
                                $crr = array();
                                $crr['area_name'] = $c[$key];
                                $crr['area_p_id'] = $id;
                                $crr['area_show'] = 1;
                                $crr['area_addtime'] = time();
                                $id = $this->zhou_sima->db_add_re_insert_id('zhou_area',$crr);
                            }
                        }
                    }
                }else{
                    if ($b[$key] !='') {
                        $d = $b[$key];
                        $ress = $this->zhou_sima->get_one('zhou_area',"area_name = '$d'");
                        if ($ress) {
                            if ($c[$key] != '') {
                                $crr = array();
                                $crr['area_name'] = $c[$key];
                                $crr['area_name'] = $c[$key];
                                $crr['area_p_id'] = $ress['area_id'];
                                $crr['area_show'] = 1;
                                $crr['area_addtime'] = time();
                                $this->zhou_sima->db_add_re_insert_id('zhou_area',$crr);
                            }
                        }else{
                            $brr = array();
                            $brr['area_name'] = $b[$key];
                            $brr['area_p_id'] = $res['area_id'];
                            $brr['area_show'] = 1;
                            $brr['area_addtime'] = time();
                            $id = $this->zhou_sima->db_add_re_insert_id('zhou_area',$brr);
                            if ($id !=false) {
                                if ($c[$key] != '') {
                                    $crr = array();
                                    $crr['area_name'] = $c[$key];
                                    $crr['area_p_id'] = $id;
                                    $crr['area_show'] = 1;
                                    $crr['area_addtime'] = time();
                                    $id = $this->zhou_sima->db_add_re_insert_id('zhou_area',$crr);
                                }
                            }
                        }
                    }

                }
            }
            $this->adminMsg(lang('success'), url('admin/zhou_position/area_list'), 3, 1, 1);
            die();
        }
        $this->view->display('admin/zhou_area_edit');
    }
     //区域模板下载
    public function down_areaAction(){
        $filename = "area.xls";
        $file = fopen(FCPATH.'uploadfiles/model/'.$filename,"r");
        //
        Header("Content-type:application/octet-stream");
        Header("Accept-Ranges:bytes");
        header("Content-Type:application/msexcel");
        Header("Accept-Length:".filesize(FCPATH.'uploadfiles/model/'.$filename));
        Header("Content-Disposition:attachment;filename=".$filename);
        echo
        fread($file,filesize(FCPATH.'uploadfiles/model/'.$filename));
        fclose($file);
        $this->adminMsg(lang('success'), url('sima/zhou_position/area_list'),2, 1,1);
    }

    //区域管理
    public function area_listAction(){
        //判断城市
        if($this->get('diqu')){
            $arr = $this->get('diqu');
            if ($arr=='省份'){
                $arr = '市区';
            }elseif ($arr == '市区'){
                $arr = '县/区';
            }
        }
        //接收城市ID => cat_id
        if($this->get('cat_id')){
            $cat_id = $this->get('cat_id');
            $urlparam = array('page' => '{page}','cat_id'=>$cat_id,'diqu'=>$arr);
        }else{
            $cat_id = '0';
            $urlparam = array('page' => '{page}','diqu'=>$arr);
        }
        //下级列表
        $table = 'zhou_area';
        $where = 'area_show = 1 and area_p_id ='.$cat_id; //查找下级城市
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        $pagelist = $this->instance('pagelist');    //加载分页类
        $pagesize = 10;
        $pagelist->loadconfig();
        $total = $this->zhou_sima->get_count($table,'area_id',$where);
        $pagelist = $pagelist->total($total)->url(url('admin/zhou_position/area_list', $urlparam))->num($pagesize)->page($page)->output();
        $res = $this->zhou_sima->get_one_page($page,$pagesize,$total,$table,$where);
        if ($res) {//当下级不为空时
            foreach ($res as $key => $value){
                if ($value['area_p_id']) {
                    $parentid = $value['area_p_id'];
                }

            }
        }else{
            $parentid = $cat_id;
        }

        //判断城市
        $arr =array();
        foreach ($res as $key=>$value){
            if ($value['area_p_id']==0){
                $arr = '省份';
            }
        }
        if($this->get('diqu')){
            $arr = $this->get('diqu');
            if ($arr=='省份'){
                $arr = '市区';
            }elseif ($arr == '市区'){
                $arr = '县/区';
            }
        }
        $this->view->assign(array(
            'res'=>$res,
            'pagelist'=>$pagelist,
            'diqu'=>$arr,
            'parentid'=>$parentid
        ));
        $this->view->display('admin/zhou_area_list');
    }

    //区域删除
    public  function area_delAction(){
        $del_id = (int)$this->get('del_id');
        if ($del_id) {
            $table = 'zhou_area';
            $where = "area_id =".$del_id;
            $cat_id = $this->zhou_sima->get_cate_all($table,'area_p_id ='.$del_id);
            $res = $this->zhou_sima->del($table,$where);
            if ($cat_id) {
                foreach ($cat_id as $key => $value) {
                    $del_cat_id = $this->zhou_sima->del('zhou_area','area_id ='.$value['area_id']);
                }
                if ($del_cat_id || $cat_id) {
                    $this->adminMsg(lang('success'), url('admin/zhou_position/ area_list'), 3, 1, 1);

                }

            }
            if ($res){
                $this->adminMsg(lang('success'), url('admin/zhou_position/ area_list'), 3, 1, 1);
            }

        }
    }

    //从业时间
    public  function work_time_listAction(){
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        $pagelist = $this->instance('pagelist');    //加载分页类
        $pagesize = 10;
        $pagelist->loadconfig();
        $urlparam = array('page' => '{page}');
        $total = $this->zhou_sima->get_count('zhou_work_time','time_id',' 1');
        $pagelist = $pagelist->total($total)->url(url('admin/zhou_position/work_time_list', $urlparam))->num($pagesize)->page($page)->output();
        $res = $this->zhou_sima->get_one_page($page,$pagesize,$total,'zhou_work_time',' 1 order by time_ord asc');
        //排序
             $id = array();
             $ord = array();
             $id = $_POST['time_id'];
             $ord = $_POST['ord'];
            if ($id && $ord) {
                foreach ($id as $key => $value) {
                    $arr = array();
                    $arr['time_ord'] = $ord[$key];
                    $list= $this->zhou_sima->update_table('zhou_work_time',$arr,'time_id ='.$value);
                }
                if ($list) {
                    $this->adminMsg(lang('success'),url('admin/zhou_position/work_time_list'),2,1,1);
                }
            }
        $this->view->assign(array(
            'res'=>$res,
            'pagelist'=>$pagelist
        ));
        $this->view->display('admin/zhou_worktime_list');
    }
    //
    public function work_time_editAction(){
        $id = $this->get('edit_id');
        $table = 'zhou_work_time';
        if ($id) {
            $res = $this->zhou_sima->get_one($table,'time_id ='.$id);
//            var_dump($res);die;
            $this->view->assign('res',$res);
        }
        if ($_POST) {
            $data = array();
            $data['time_name'] = $_POST['name'];
            $data['time_show'] = $_POST['show'];
            $data['time_ord'] = $_POST['ord'];
            $data['addtime'] = time();
            if ($id) {//编辑
                $list = $this->zhou_sima->update_table($table,$data,'time_id ='.$id);
            }else{
                $list = $this->zhou_sima->db_add($table,$data);
            }
            if ($list) {
                $this->adminMsg(lang('success'),url('admin/zhou_position/work_time_list'),2,1,1);
            }
        }
        $this->view->display('admin/zhou_worktime_edit');
    }
    //删除
    public  function work_time_delAction(){
        $del_id = $this->get('del_id');
        if ($del_id){
            $res = $this->zhou_sima->del('zhou_work_time','time_id ='.$del_id);
            if($res){
                $this->adminMsg(lang('success'),url('admin/zhou_position/work_time_list'),2,1,1);
            }
        }
    }

    //薪资管理
    public  function work_salary_listAction(){
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        $pagelist = $this->instance('pagelist');    //加载分页类
        $pagesize = 10;
        $pagelist->loadconfig();
        $urlparam = array('page' => '{page}');
        $total = $this->zhou_sima->get_count('zhou_salary','salary_id',' 1');
        $pagelist = $pagelist->total($total)->url(url('admin/zhou_position/work_time_list', $urlparam))->num($pagesize)->page($page)->output();
        $res = $this->zhou_sima->get_one_page($page,$pagesize,$total,'zhou_salary',' 1 order by salary_ord asc');
        //排序
        $id = $_POST['salary_id'];
        $ord = $_POST['ord'];
        if ($id && $ord) {
            foreach ($id as $key => $value) {
                $arr = array();
                $arr['salary_ord'] = $ord[$key];
                $list= $this->zhou_sima->update_table('zhou_salary',$arr,'salary_id ='.$value);
            }
            if ($list) {
                $this->adminMsg(lang('success'),url('admin/zhou_position/work_salary_list'),2,1,1);
            }
        }
        $this->view->assign(array(
            'res'=>$res,
            'pagelist'=>$pagelist
        ));
        $this->view->display('admin/zhou_salary_list');
    }
    //编辑、添加
    public function work_salary_editAction(){
        $id = $this->get('edit_id');
        $table = 'zhou_salary';
        if ($id) {
            $res = $this->zhou_sima->get_one($table,'salary_id ='.$id);
            $this->view->assign('res',$res);
        }
        if ($_POST) {
//            var_dump($_POST);die;
            $data = array();
            $data['salary_name'] = $_POST['name'];
            $data['salary_ord']  = $_POST['ord'];
            $data['addtime'] = time();
            if ($id) {//编辑
                $list = $this->zhou_sima->update_table($table,$data,'salary_id ='.$id);
            }else{ //添加
                $list = $this->zhou_sima->db_add($table,$data);
            }
            if ($list) {
                $this->adminMsg(lang('success'),url('admin/zhou_position/work_salary_list'),2,1,1);
            }
        }
        $this->view->display('admin/zhou_salary_edit');
    }
    //删除
    public  function work_salary_delAction(){
        $del_id = $this->get('del_id');
        if ($del_id){
            $res = $this->zhou_sima->del('zhou_salary','salary_id ='.$del_id);
            if($res){
                $this->adminMsg(lang('success'),url('admin/zhou_position/work_salary_list'),2,1,1);
            }
        }
    }

}